﻿#region License
/*
 *CopyRight (c) By Denuvead All Rights Reserved
 *CreateDate:
 *Info:
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using PriErp.Data;
using PriErp.Tools.Extensions;

namespace PriErp.Dal
{
    /// <summary>
    /// 用户操作日志
    /// </summary>
    public static class UserActionsDAL
    {
        #region BasicMethod
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static bool Add(int UserID, ActionType aType)
        {
            string strSql = "insert into UserActions(UserID,Type) values (@UserID,@Type)";
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@UserID", UserID),
                    new SQLiteParameter("@Type",(int)aType)};
            return DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除用户时删除操作记录
        /// </summary>
        public static bool Delete(int UserID)
        {
            string strSql = "delete from UserActions where UserID=@UserID";
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@UserID", UserID)
            };
            return DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 获取指定用户指定类型最近操作时间
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="aType">操作类型</param>
        /// <returns></returns>
        public static DateTime GetActionTime(int UserID, ActionType aType)
        {
            string strSql = "select ifnull(max(ActionTime),(select createtime from users where userid=@UserID)) as actiontime from useractions userid=@UserID and Type=@Type";
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@UserID", UserID),
                    new SQLiteParameter("@Type",(int)aType)};
            return DbHelperSQLite.ExecuteSql(strSql, parameters).ToStringOrNull().ToDateTime(DateTime.Now);
        }
        #endregion BasicMethod
    }
}
